#ifndef _MEMORY_H
#define _MEMORY_H

/* system includes */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

/* extern types */
extern unsigned long mem_allocated;
extern void *xalloc(unsigned long size);
extern void *zalloc(unsigned long size);
extern void xfree(void *p);

/* Global alloc macro */
#define ALLOC(n) (xalloc(n))

/* Local defines */
#ifdef _DEBUG_

int debug;

#define MAX_ALLOC_LIST 2048

#define MALLOC(n)    ( dbg_malloc((n), \
                      (__FILE__), (char *)(__FUNCTION__), (__LINE__)) )
#define FREE(b)      ( dbg_free((b), \
                      (__FILE__), (char *)(__FUNCTION__), (__LINE__)) )
#define REALLOC(b,n) ( dbg_realloc((b), (n), \
                      (__FILE__), (char *)(__FUNCTION__), (__LINE__)) )
#define STRDUP(n)    ( dbg_strdup((n), \
                      (__FILE__), (char *)(__FUNCTION__), (__LINE__)) )

/* Memory debug prototypes defs */
extern char *dbg_malloc(unsigned long, char *, char *, int);
extern int dbg_free(void *, char *, char *, int);
extern void *dbg_realloc(void *, unsigned long, char *, char *, int);
extern char *dbg_strdup(char *, char *, char *, int);
extern void dbg_free_final(char *);

#else

#define MALLOC(n)    (zalloc(n))
#define FREE(p)      (xfree(p))
#define REALLOC(p,n) (realloc((p),(n)))
#define STRDUP(n)    (strdup(n))

#endif

/* Common defines */
#define FREE_PTR(P) if((P)) FREE((P));

#endif

